package com.uzai.console.service.locallive.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.locallive.activity.LocalActivityQuery;
import com.uzai.console.dto.locallive.activity.LocalLiveActivitySet;
import com.uzai.console.dto.locallive.activity.LocalLiveActivitySetDto;
import com.uzai.console.dto.locallive.activity.LocalLiveActivitySetRedis;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.LocalLiveActivity;
import com.uzai.console.mapper.LocalLiveActivityMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.locallive.LocalLiveActivityService;
import com.uzai.console.vo.locallive.activity.LocalLiveActivitySetVo;
import com.uzai.console.vo.locallive.activity.LocalLiveActivityVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 饿了么配置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class LocalLiveActivityServiceImpl implements LocalLiveActivityService {

    Logger logger = LoggerFactory.getLogger(LocalLiveActivityServiceImpl.class);

    @Autowired
    private LocalLiveActivityMapper localLiveActivityMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    /**
     * 查询所有本地生活活动列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param localLiveActivityQuery
     * @return null
     */
    public LocalLiveActivitySetVo findLocalLiveActivityList(LocalActivityQuery localLiveActivityQuery){

        if(localLiveActivityQuery.getPlatform() == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入平台");
        }

        //查询本地生活本地生活设置
        LocalLiveActivitySetVo localLiveActivitySetVo = new LocalLiveActivitySetVo();
        List<LocalLiveActivitySet> localLiveActivitySetList = new ArrayList<>();
        Integer platform = Tools.getInteger(localLiveActivityQuery.getPlatform());
        //运营商配置的本地生活设置
        String locallive_localLive_activity_set_key = null;
        if(platform.intValue() == 0){ //美团
            locallive_localLive_activity_set_key = MerConfigKeyEnum.LOCALLIVE_MEITUAN_ACTIVITY_SET.getValue();
        }else if(platform.intValue() == 1){ //饿了么
            locallive_localLive_activity_set_key = MerConfigKeyEnum.LOCALLIVE_ELM_ACTIVITY_SET.getValue();
        }

        MerBaseConfig locallive_localLive_activity_set = merBaseConfigMapper.selectByMerId(localLiveActivityQuery.getMerId(), locallive_localLive_activity_set_key);
        if(locallive_localLive_activity_set != null){
            if(StringUtils.isNotBlank(locallive_localLive_activity_set.getVal())){
                LocalLiveActivitySetRedis localLiveActivitySetRedis = JSONObject.parseObject(locallive_localLive_activity_set.getVal(), LocalLiveActivitySetRedis.class);
                if(localLiveActivitySetRedis != null){
                    localLiveActivitySetVo.setUseOfficialFlag(localLiveActivitySetRedis.getUseOfficialFlag());
                    localLiveActivitySetList = localLiveActivitySetRedis.getLocalLiveActivitySetList();
                }
            }
        }

        //新建返回到页面的LIST对象
        List<LocalLiveActivityVo> localLiveActivityVoList = new ArrayList<>();
        List<LocalLiveActivity> localLiveActivityList =  localLiveActivityMapper.findByList(localLiveActivityQuery);
        if(localLiveActivityList != null && localLiveActivityList.size() > 0){
            for (LocalLiveActivity localLiveActivity : localLiveActivityList){
                LocalLiveActivityVo localLiveActivityVo = new LocalLiveActivityVo();
                BeanUtils.copyProperties(localLiveActivity, localLiveActivityVo);
                if(localLiveActivitySetList != null && localLiveActivitySetList.size() > 0){
                    for (LocalLiveActivitySet localLiveActivitySet : localLiveActivitySetList){
                        if(localLiveActivity.getId().longValue() == localLiveActivitySet.getId().longValue()){//找到该本地生活活动配置
                            localLiveActivityVo.setSort(localLiveActivitySet.getSort());
                            localLiveActivityVo.setShowFlag(localLiveActivitySet.getShowFlag());
                        }
                    }
                }

                localLiveActivityVoList.add(localLiveActivityVo);
            }
        }

        //将返回对象排序(如果没有，则默认id排序降序)
        Tools.sort(localLiveActivityVoList, "sort", "asc");
        localLiveActivitySetVo.setMeituanActivityVoList(localLiveActivityVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return localLiveActivitySetVo;
    }

    /**
     * 修改本地生活活动设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param localLiveActivitySetDto
     * @return null
     */
    public void updateLocalLiveActivitySet(LocalLiveActivitySetDto localLiveActivitySetDto){

        Integer platform = Tools.getInteger(localLiveActivitySetDto.getPlatform());
        if(platform == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入平台");
        }

        int now = DateUtil.getNowTime();

        LocalLiveActivitySetRedis localLiveActivitySetRedis = new LocalLiveActivitySetRedis();
        localLiveActivitySetRedis.setUseOfficialFlag(localLiveActivitySetDto.getUseOfficialFlag());
        localLiveActivitySetRedis.setLocalLiveActivitySetList(localLiveActivitySetDto.getLocalLiveActivitySetList());

        //修改本地生活本地生活活动配置
        String localLive_activity_set_key = null;
        String localLive_activity_set_key_redis = null;
        if(platform.intValue() == 0){ //美团
            localLive_activity_set_key = MerConfigKeyEnum.LOCALLIVE_MEITUAN_ACTIVITY_SET.getValue();
            localLive_activity_set_key_redis = String.format(RedisCacheKeyEnum.LOCALLIVE_MEITUAN_ACTIVITY_SET.getKey(), localLiveActivitySetDto.getMerId());
        }else if(platform.intValue() == 1){ //饿了么
            localLive_activity_set_key = MerConfigKeyEnum.LOCALLIVE_ELM_ACTIVITY_SET.getValue();
            localLive_activity_set_key_redis = String.format(RedisCacheKeyEnum.LOCALLIVE_ELM_ACTIVITY_SET.getKey(), localLiveActivitySetDto.getMerId());
        }

        MerBaseConfig localLive_activity_set = merBaseConfigMapper.selectByMerId(localLiveActivitySetDto.getMerId(), localLive_activity_set_key);

        if (localLive_activity_set != null){
            localLive_activity_set.setVal(Tools.getStrEmpty(JSONObject.toJSONString(localLiveActivitySetRedis)));
            localLive_activity_set.setUpdateTime(now);
            merBaseConfigMapper.update(localLive_activity_set);
        } else { //增加本地生活本地生活活动配置
            localLive_activity_set = new MerBaseConfig();
            localLive_activity_set.setId(IdWorker.getId());
            localLive_activity_set.setMerId(localLiveActivitySetDto.getMerId());
            localLive_activity_set.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            localLive_activity_set.setKey(localLive_activity_set_key);
            localLive_activity_set.setVal(Tools.getStrEmpty(JSONObject.toJSONString(localLiveActivitySetRedis)));
            localLive_activity_set.setCreateTime(now);
            localLive_activity_set.setUpdateTime(now);
            merBaseConfigMapper.insert(localLive_activity_set);
        }

        //更新redis
        redisTemplate.opsForValue().set(localLive_activity_set_key_redis, Tools.getStrEmpty(JSONObject.toJSONString(localLiveActivitySetRedis)));

    }



}
